package com.orange.studio.dbk.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.orange.studio.dbk.R;
import com.orange.studio.dbk.dto.SongDTO;
import com.orange.studio.dbk.listeners.MainHandler;

public class ListFavioritesSongAdapter extends BaseAdapter {
	private List<SongDTO> mData=null;
	private Activity mActivity =null;
	private LayoutInflater mInflater=null;
	private MainHandler mHandler=null;
	public ListFavioritesSongAdapter(Activity activity,MainHandler handler){
		mActivity=activity;
		mInflater=(LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mData=new ArrayList<SongDTO>();
		mHandler=handler;
	}
	public void updateListData(List<SongDTO> data){
		mData.clear();
		mData.addAll(data);
		notifyDataSetChanged();
	}
	public void insertListData(List<SongDTO> data){	
		mData.addAll(data);
		notifyDataSetChanged();
	}
	public void removeUnLikeSongs(int id){
		try {
			for (int j = 0; j < getListData().size(); j++) {
				if(mData.get(j).getId()==id){
					mData.remove(j);
					notifyDataSetChanged();
					break;
				}
			}
		} catch (Exception e) {
		}
	}
	public List<SongDTO> getListData(){
		return mData;
	}
	public void cleanListData(){
		mData.clear();
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int index) {
		return mData.get(index);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	public class ViewHolder{
		public TextView mSongID;
		public TextView mSongName;
		public TextView mSongLyric;
		public ImageView mLikedBtn;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder mViewHolder;
		if(convertView==null){
			convertView=mInflater.inflate(R.layout.song_item_layout, parent, false);
			mViewHolder=new ViewHolder();
			mViewHolder.mSongID=(TextView) convertView.findViewById(R.id.songID);
			mViewHolder.mSongName=(TextView) convertView.findViewById(R.id.songName);
			mViewHolder.mSongLyric=(TextView) convertView.findViewById(R.id.songLyric);
			mViewHolder.mLikedBtn=(ImageView) convertView.findViewById(R.id.likedBtn);
			convertView.setTag(mViewHolder);
		}else{
			mViewHolder=(ViewHolder) convertView.getTag();
		}
		final SongDTO song=mData.get(position);
		updateLikeBtnBackground(mViewHolder, song);
		mViewHolder.mSongID.setText(String.valueOf(song.getId()));
		mViewHolder.mSongName.setText(song.getName());
		mViewHolder.mSongLyric.setText(song.getLyricclean());
		mViewHolder.mLikedBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {	
//				if(song.getLiked()<1){
//					song.setLiked(1);
//				}else{
//					song.setLiked(0);
//				}
				mHandler.setLiked(song);				
				updateLikeBtnBackground(mViewHolder, song);
			}
		});
		return convertView;
	}
	private void updateLikeBtnBackground(ViewHolder mViewHolder,
			final SongDTO song) {
		if(song.getLiked()<1){
			mViewHolder.mLikedBtn.setBackgroundResource(R.drawable.icon);
		}else{
			mViewHolder.mLikedBtn.setBackgroundResource(R.drawable.ic_launcher);
		}
	}

}
